package com.alm.token.ui.activity;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.BaseDialogBean;
import com.alm.token.ui.data.bean.EggRankingData;
import com.alm.token.ui.data.bean.EggWalletLogData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.SuperColorEggData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.fragmentDialog.NoticeDialogFragment;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.SuperColorEggView;
import com.alm.token.ui.viewpagefragment.EggWalletLog2PageFrag;
import com.alm.token.ui.viewpagefragment.EggWalletLogPageFrag;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LanguageUtils;
import com.alm.token.utils.LogUtil;
import com.alm.token.utils.Utils;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yzq.zxinglibrary.encode.CodeCreator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.alm.token.utils.CommonUtil.formatSeconds;

public class SuperColorEggActivity extends BaseActivity implements SuperColorEggView<SuperColorEggData> {
    @BindView(R.id.tv_reword)
    TextView tvReword;
    @BindView(R.id.tv_reword_value)
    TextView tvRewordValue;
    @BindView(R.id.tv_reword_moneyadress)
    ImageView tvRewordMoneyAdress;
    @BindView(R.id.img_qdinvite)
    TextView imgQdinvite;
    @BindView(R.id.img_wfguz)
    TextView imgWfguz;
    @BindView(R.id.leftItem)
    ImageView leftItem;
    @BindView(R.id.img_panghangban)
    TextView imgPanghangban;
    @BindView(R.id.rly_downtime_body)
    RelativeLayout rlyDownTimeBody;
    @BindView(R.id.tv_endtimeing)
    TextView tvEndtimeing;
    @BindView(R.id.tv_opennexttime)
    TextView tvOpenNextTime;
    @BindView(R.id.tv_buyegg)
    TextView tvBuyegg;
    @BindView(R.id.tv_numTotal)
    TextView tvNumTotal;
    @BindView(R.id.tv_totalMoney)
    TextView tvTotalMoney;
    @BindView(R.id.tv_egg)
    TextView tvEgg;
    @BindView(R.id.tv_getMoney)
    TextView tvGetMoney;
    @BindView(R.id.tv_smiototal)
    TextView tvSmiototal;
    @BindView(R.id.tv_getsmio)
    TextView tvGetsmio;
    @BindView(R.id.tv_buy_lastusername)
    TextView tvBuyLastusername;

    @BindView(R.id.ly_buy_lastusernamebody)
    LinearLayout lyLastusernamebody;

    @BindView(R.id.tv_nextHalveNum_tv1)
    TextView nextHalveNum_tv1;
    @BindView(R.id.tv_nextHalveNum_tv3)
    TextView nextHalveNum_tv3;
    @BindView(R.id.tv_nextHalveNum)
    TextView nextHalveNum_tv;
    @BindView(R.id.tv_nextHalveNum_tv2)
    TextView nextHalveNum_tv2;
    @BindView(R.id.tv_nextHalveNum_tv5)
    TextView nextHalveNum_tv5;

    private List<SuperColorEggData.DataBean.EggGoodsBean> eggGoodsBeans = new ArrayList<>();

    private String moneyAddress;
    private String pondMoney = "0.0000", totalValue = "0.00";
    private String registerUrl, nextHalveNum;

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_super_color_egg;
    }

    @Override
    protected void initVariables() {
        tvEndtimeing.setVisibility(View.GONE);
        tvOpenNextTime.setVisibility(View.GONE);
        rlyDownTimeBody.setVisibility(View.GONE);
        tvBuyLastusername.setVisibility(View.GONE);
        lyLastusernamebody.setVisibility(View.GONE);
//        imgQdinvite.setVisibility(View.GONE);
        tvEndtimeing.setText(getString(R.string.easteregg_hint_time, formatSeconds(times)));//结束倒计时
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void loadData() {
        preenter.getSuperColorEggData();
    }

    @OnClick({R.id.rly_myegg_subscribe_item,R.id.tv_reword_moneyadress, R.id.img_wfguz, R.id.leftItem, R.id.img_panghangban, R.id.rly_myegg_item, R.id.rly_myeggaward_item, R.id.rly_mysmio_item, R.id.rly_mysmiotoken_item})
    public void onViewClicked(View view) {
        Intent intent1 = new Intent(this, VipOrderListActivity.class);
        switch (view.getId()) {
            case R.id.rly_myegg_subscribe_item:
                //彩蛋预约
                Intent intent = new Intent(SuperColorEggActivity.this, SubscribeOrderEggActivity.class);
                intent.putExtra("eggGoodsBeans", (Serializable) eggGoodsBeans);
                startActivity(intent);

                break;
            case R.id.tv_reword_moneyadress:
                Intent intent2 = new Intent(SuperColorEggActivity.this, JackpotAddressActivity.class);
                intent2.putExtra("moneyAdress", "https://btc.com/" + moneyAddress);
                startActivity(intent2);
//                showToast("https://btc.com/"+moneyAddress);
                break;
            case R.id.img_wfguz:
//                showToast("玩法规则click");
                userMsgBean = new UserWalletData.DataBean.UserMsgBean();
                userMsgBean.setTitle(getString(R.string.easteregg_hint_wanfaroal));
                userMsgBean.setContent(getString(R.string.easteregg_hint_rolemsg));
                userMsgBean.setEnTitle(getString(R.string.easteregg_hint_wanfaroal));
                userMsgBean.setEnContent(getString(R.string.easteregg_hint_rolemsg));
                showCodeDialog();
                break;
            case R.id.leftItem:
                super.onBackPressed();
                break;
            case R.id.img_panghangban:
//                showToast("排行榜");
                intent1.putExtra("intentType", 6);
                startActivity(intent1);
                break;
            case R.id.rly_myegg_item:
                intent1.putExtra("intentType", 5);
                intent1.putExtra("INTENT_EGGLOG_TYPE", EggWalletLogPageFrag.INTENT_EGGLOG_MYEGG);
                startActivity(intent1);
                break;
            case R.id.rly_mysmio_item:
                intent1.putExtra("intentType", 5);
                intent1.putExtra("INTENT_EGGLOG_TYPE", EggWalletLogPageFrag.INTENT_EGGLOG_MYSMIO);
                startActivity(intent1);
                break;

            case R.id.rly_mysmiotoken_item:
//                showToast("排行榜");
                intent1.putExtra("intentType", 7);
                intent1.putExtra("INTENT_EGGLOG_TYPE", EggWalletLog2PageFrag.INTENT_EGGLOG_PARTANERTOKEN);
                startActivity(intent1);
                break;
            case R.id.rly_myeggaward_item:
                intent1.putExtra("intentType", 7);
                intent1.putExtra("INTENT_EGGLOG_TYPE", EggWalletLog2PageFrag.INTENT_EGGLOG_MYEGGTOKEN);
                startActivity(intent1);
                break;


        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_buyegg:
//                if (ISDEBUG) {

//                if (MyApp.getUserData() != null && MyApp.getUserData().getData() != null && !MyApp.getUserData().getData().isPayPwd()) {
//                    jumpToLoginAct(4);
//                    break;
//                }

                Intent intent = new Intent(SuperColorEggActivity.this, BuySEggActivity.class);
                intent.putExtra("eggGoodsBeans", (Serializable) eggGoodsBeans);
                startActivity(intent);
//                } else {
//                    showToast(getString(R.string.currency_hint1_warn));
//                }
                break;
            case R.id.img_qdinvite:

                if (EmptyUtils.isNotEmpty(imgQdinvite.getTag()) && (int) imgQdinvite.getTag() == 1) {
                    if (EmptyUtils.isNotEmpty(registerUrl)) {
                        showpopwindow(registerUrl);
                    } else {
                        showToast(getString(R.string.network_loading_hint1));
                    }
                } else {
                    showToast(getString(R.string.easteregg_hint_noopen));
                }
                break;
            case R.id.tv_savebitmap:
                if (bodyBitmap == null) {
                    showToast(getString(R.string.network_loading_hint1));
                    break;
                }
                //存储权限
                RxPermissions rxPermissions = new RxPermissions(this);
                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Observer<Boolean>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(Boolean granted) {
                                if (granted) {
                                    if (saveImageToGallery(SuperColorEggActivity.this, bodyBitmap, getString(R.string.easteregg_hint_qdsharename) + CommonUtil.getStringDate() + ".jpg")) {
                                        showToast(getString(R.string.profile_hint_item15));
                                        tvSavebitmap.setText(getString(R.string.profile_hint_item16));
                                    }
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });

                break;
            case R.id.tv_copy:
                if (xiazaiuri.isEmpty()) {
                    showToast(getString(R.string.network_loading_hint1));
                    break;
                }
                copyWalletAddress(xiazaiuri);
                break;
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    private CountDownTimer timer;
    private long times = 0, refreshTime = -1;


    private CountDownTimer timer1;
    private long nextEggStartTime = 0, refreshnextEggStartTime = -1;

    @Override
    public void getData(SuperColorEggData superColorEggData) {

        if (superColorEggData.getStatus() == 0) {

            if (superColorEggData.getData() != null) {
                imgQdinvite.setOnClickListener(this);
                SuperColorEggData.DataBean.EggBean eggBean = superColorEggData.getData().getEgg();
                SuperColorEggData.DataBean.EggWalletBean eggWalletBean = superColorEggData.getData().getEggWallet();
                eggGoodsBeans = superColorEggData.getData().getEggGoods();
                tvBuyLastusername.setText( (EmptyUtils.isEmpty(superColorEggData.getData().getLastUserName()) ? getString(R.string.mine_hint_empty) : EmptyUtils.returnNOTNull(superColorEggData.getData().getLastUserName())));
                moneyAddress = superColorEggData.getData().getMoneyAddress();
//                tvRewordMoneyAdress.setText(moneyAddress);//getString(R.string.tv_hint_activity_superegg_moneyadress)+": "+
                registerUrl = superColorEggData.getData().getRegisterUrl();
                nextHalveNum = superColorEggData.getData().getNextHalveNum();
                nextHalveNum_tv2.setText(nextHalveNum);
                times = superColorEggData.getData().getEggLastTime();
                nextEggStartTime = superColorEggData.getData().getNextEggStartTime();
                tvEndtimeing.setText(getString(R.string.easteregg_hint_time, formatSeconds(times)));//结束倒计时

                if (times > 10) {
                    refreshTime = times - 10;
                }
//                times = 100;
                if (EmptyUtils.isNotEmpty(timer)) {
                    timer.cancel();
                }
                timer = new CountDownTimer(times * 1000, 1000) {
                    /**
                     * 固定间隔被调用,就是每隔countDownInterval会回调一次方法onTick
                     * @param millisUntilFinished
                     */
                    @Override
                    public void onTick(long millisUntilFinished) {
                        if (millisUntilFinished / 1000 <= refreshTime && EmptyUtils.isNotEmpty(timer)) {
                            refreshTime = -1;
                            timer.onFinish();
//                            timer.cancel();
                        }
                        if (EmptyUtils.isNotEmpty(tvEndtimeing)) {
                            tvEndtimeing.setText(getString(R.string.easteregg_hint_time, formatSeconds(millisUntilFinished / 1000)));//结束倒计时
                        }
//                        tv_remaining_time.setText(formatTime(millisUntilFinished));
                    }

                    /**
                     * 倒计时完成时被调用
                     */
                    @Override
                    public void onFinish() {
                        if (EmptyUtils.isNotEmpty(preenter)) {
                            preenter.getSuperColorEggData();
                        }
//                        tv_remaining_time.setText("00:00");
                    }
                };
                if (times != 0) {
                    timer.start();
                }

                if (EmptyUtils.isNotEmpty(eggBean)) {
                    pondMoney = String.valueOf(CommonUtil.saveToPoint4WithNo(eggBean.getBtc()));
                    totalValue = String.valueOf(CommonUtil.saveToPoint4WithNo(eggBean.getBtc() * eggBean.getBtcPrice()));
//                    tvReword.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggBean.getRewardPool())));//总奖池
                    tvReword.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggBean.getBtc())));//btc总奖池
                    tvRewordValue.setText(String.valueOf("≈ $" + CommonUtil.saveToPoint2WithNo(eggBean.getBtc() * eggBean.getBtcPrice())));
                    tvNumTotal.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggBean.getSethTotal())));//当前彩蛋总数
                    tvTotalMoney.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggBean.getDynamicPool() + eggBean.getStaticPool())));//当前彩蛋总激励
                } else {
                    tvReword.setText(pondMoney);//btc总奖池
                    tvRewordValue.setText("≈ $" + totalValue);
                    tvNumTotal.setText(pondMoney);//当前彩蛋总数
                    tvTotalMoney.setText(pondMoney);//当前彩蛋总激励
                }

                if (EmptyUtils.isNotEmpty(eggWalletBean)) {
                    tvEgg.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggWalletBean.getSeth())));//我的彩蛋
                    tvSmiototal.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggWalletBean.getSmio())));//合伙人权益sMIO
                    tvGetsmio.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggWalletBean.getSmioExcitation())) + " MIO");//合伙人收益
                    tvGetMoney.setText(String.valueOf(CommonUtil.saveToPoint4WithNo(eggWalletBean.getEthGet())) + " MIO");//彩蛋激励
                }


                if (nextEggStartTime > 10) {
                    refreshnextEggStartTime = nextEggStartTime - 10;
                }
//                times = 100;
                if (EmptyUtils.isNotEmpty(timer1)) {
                    timer1.cancel();
                }
                timer1 = new CountDownTimer(nextEggStartTime * 1000, 1000) {
                    /**
                     * 固定间隔被调用,就是每隔countDownInterval会回调一次方法onTick
                     * @param millisUntilFinished
                     */
                    @Override
                    public void onTick(long millisUntilFinished) {
                        if (millisUntilFinished / 1000 <= refreshnextEggStartTime && EmptyUtils.isNotEmpty(timer1)) {
                            refreshnextEggStartTime = -1;
                            timer1.onFinish();
//                            timer1.cancel();
                        }

                        if (EmptyUtils.isNotEmpty(tvOpenNextTime)) {
                            tvOpenNextTime.setText(getString(R.string.easteregg_hint_opennexttime, formatSeconds(millisUntilFinished / 1000)));
                        }
                    }

                    /**
                     * 倒计时完成时被调用
                     */
                    @Override
                    public void onFinish() {
                        if (EmptyUtils.isNotEmpty(preenter)) {
                            preenter.getSuperColorEggData();
                        }
//                        tv_remaining_time.setText("00:00");
                    }
                };


                if (EmptyUtils.isEmpty(eggBean) && nextEggStartTime == 0) {
                    // egg == null  nextEggStartTime==0 游戏未开始 没有活动开启倒计时显示倒计时信息
                    tvBuyegg.setText(getString(R.string.easteregg_hint_noopen));
                    tvBuyegg.setOnClickListener(null);
                    tvOpenNextTime.setVisibility(View.GONE);
                    tvEndtimeing.setVisibility(View.GONE);
                    rlyDownTimeBody.setVisibility(View.GONE);
                    tvBuyLastusername.setVisibility(View.GONE);
                    lyLastusernamebody.setVisibility(View.GONE);
//                    imgQdinvite.setVisibility(View.GONE);
                    imgQdinvite.setTag(0);
                } else if (EmptyUtils.isEmpty(eggBean)) {
                    // egg == null  nextEggStartTime!=0 ，游戏未开始 有活动开启倒计时显示倒计时信息
                    tvOpenNextTime.setText(getString(R.string.easteregg_hint_opennexttime, formatSeconds(nextEggStartTime)));
                    tvBuyegg.setText(getString(R.string.easteregg_hint_noopen));
                    tvBuyegg.setOnClickListener(null);
                    timer1.start();
                    tvOpenNextTime.setVisibility(View.VISIBLE);
                    tvEndtimeing.setVisibility(View.GONE);
                    rlyDownTimeBody.setVisibility(View.GONE);
                    tvBuyLastusername.setVisibility(View.GONE);
                    lyLastusernamebody.setVisibility(View.GONE);
//                    imgQdinvite.setVisibility(View.GONE);
                    imgQdinvite.setTag(0);
                } else if (EmptyUtils.isNotEmpty(eggBean) && eggBean.getStatus() == 1) {
                    //status : 1 // 状态 0 未开启 1 开启 2 已结束
                    tvBuyegg.setText(getString(R.string.easteregg_hint_buy));
                    tvBuyegg.setOnClickListener(this);
                    tvOpenNextTime.setVisibility(View.GONE);
                    tvEndtimeing.setVisibility(EmptyUtils.isEmpty(superColorEggData.getData().getLastUserName()) ? View.GONE : View.VISIBLE);
                    rlyDownTimeBody.setVisibility(View.VISIBLE);
                    tvBuyLastusername.setVisibility(View.VISIBLE);
                    lyLastusernamebody.setVisibility(View.VISIBLE);
//                    imgQdinvite.setVisibility(View.VISIBLE);
                    imgQdinvite.setTag(1);
                } else if (EmptyUtils.isNotEmpty(eggBean) && eggBean.getStatus() == 0) {
                    //status : 1 // 状态 0 未开启 1 开启 2 已结束
                    if (nextEggStartTime == 0) {
                        tvOpenNextTime.setVisibility(View.GONE);
                        tvBuyegg.setText(getString(R.string.easteregg_hint_noopen));
                    } else {
                        tvOpenNextTime.setVisibility(View.VISIBLE);
                        tvOpenNextTime.setText(getString(R.string.easteregg_hint_opennexttime, formatSeconds(nextEggStartTime)));
                        tvBuyegg.setText(getString(R.string.easteregg_hint_noopen));
                        timer1.start();
                    }
                    tvBuyegg.setOnClickListener(null);
                    tvEndtimeing.setVisibility(View.GONE);
                    rlyDownTimeBody.setVisibility(View.GONE);
                    tvBuyLastusername.setVisibility(View.GONE);
                    lyLastusernamebody.setVisibility(View.GONE);
//                    imgQdinvite.setVisibility(View.GONE);
                    imgQdinvite.setTag(0);
                } else if (EmptyUtils.isNotEmpty(eggBean) && eggBean.getStatus() == 2) {
                    //status : 1 // 状态 0 未开启 1 开启 2 已结束
                    // "calStatus":2 // 结算状态 2 未结算 1 结算完成
                    if (eggBean.getCalStatus() == 2) {
                        tvBuyegg.setText(getString(R.string.easteregg_hint_stoped));
                        tvOpenNextTime.setVisibility(View.GONE);
                        tvEndtimeing.setVisibility(View.GONE);
                        rlyDownTimeBody.setVisibility(View.GONE);
                        tvBuyLastusername.setVisibility(View.VISIBLE);
                        lyLastusernamebody.setVisibility(View.VISIBLE);
//                        timer1.start();
                    } else {
                        tvEndtimeing.setVisibility(View.GONE);
                        rlyDownTimeBody.setVisibility(View.GONE);
                        tvBuyLastusername.setVisibility(View.VISIBLE);
                        lyLastusernamebody.setVisibility(View.VISIBLE);
                        if (nextEggStartTime != 0) {
                            tvOpenNextTime.setVisibility(View.VISIBLE);
                            tvOpenNextTime.setText(getString(R.string.easteregg_hint_opennexttime, formatSeconds(nextEggStartTime)));
                            tvBuyegg.setText(getString(R.string.easteregg_hint_stoped2));
                            timer1.start();
                        } else {
                            tvOpenNextTime.setVisibility(View.GONE);
                            tvBuyegg.setText(getString(R.string.easteregg_hint_stoped2));
                        }
                    }
//                    imgQdinvite.setVisibility(View.GONE);
                    imgQdinvite.setTag(0);
//                    else {
//                        if (nextEggStartTime == 0) {
//                            tvBuyegg.setText(getString(R.string.easteregg_hint_noopen));
//                            tvEndtimeing.setVisibility(View.GONE);
//                            rlyDownTimeBody .setVisibility(View.GONE);
//                            tvBuyLastusername .setVisibility(View.GONE);
//                        } else {
//                            tvBuyegg.setText(getString(R.string.easteregg_hint_opennexttime, formatSeconds(nextEggStartTime)));
//                            timer1.start();
//                            tvEndtimeing.setVisibility(View.GONE);
//                            rlyDownTimeBody .setVisibility(View.GONE);
//                            tvBuyLastusername .setVisibility(View.GONE);
//                        }
//                    }
                    tvBuyegg.setOnClickListener(null);
                }
            }

        } else showToast(superColorEggData.getMsg());
    }

    @Override
    public void getEggAddData(RegisterData registerData) {

    }

    @Override
    public void getEggRankingData(EggRankingData eggRankingData) {

    }

    @Override
    public void getEggWalletLogData(EggWalletLogData eggWalletLogData) {

    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

//        showWaitDialog();
    }

    @Override
    public void hideProgress() {

//        hideWaitDialog();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (EmptyUtils.isNotEmpty(preenter)) {
            preenter.getSuperColorEggData();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EmptyUtils.isNotEmpty(timer)) {
            timer.cancel();
        }
        if (EmptyUtils.isNotEmpty(timer1)) {
            timer1.cancel();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EmptyUtils.isNotEmpty(timer)) {
            timer.cancel();
            timer = null;
        }
        if (EmptyUtils.isNotEmpty(timer1)) {
            timer1.cancel();
            timer1 = null;
        }
    }

    UserWalletData.DataBean.UserMsgBean userMsgBean;

    /***  弹出提示公告  ***/
    private void showCodeDialog() {
        NoticeDialogFragment dialogFragment = new NoticeDialogFragment();
        dialogFragment.setDialogBean(new BaseDialogBean("eggroal", userMsgBean));

        dialogFragment.setListener(new NoticeDialogFragment.ClickListener() {
            @Override
            public void Listener(String name, int potion) {
//                startActivity(BillActivity.class);
            }
        });

        dialogFragment.show(getSupportFragmentManager(), "NoticeDialogFragment");
        getFragmentManager().executePendingTransactions();
    }


    /**
     * 将毫秒转化为 分钟：秒 的格式
     *
     * @param millisecond 毫秒
     * @return
     */
    public String formatTime(long millisecond) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
        String hms = formatter.format(millisecond);
        return hms;
    }

    PopupWindow p;

    private void showpopwindow(String uri) {
        xiazaiuri = uri;
        final View inflate = View.inflate(this, R.layout.layout_eggshare_qrcode, null);
        imgAdressqrcode = inflate.findViewById(R.id.img_adressqrcode);
        lyQrcodeBody = inflate.findViewById(R.id.ly_qrcodebody);

        if (LanguageUtils.getSetIndex() == 1) {
            lyQrcodeBody.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_bg1));
        } else if (LanguageUtils.getSetIndex() == 3) {
            lyQrcodeBody.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_bg3));
        } else if (LanguageUtils.getSetIndex() == 4) {
            lyQrcodeBody.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_bg4));
        } else {
            lyQrcodeBody.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_bg2));
        }

        TextView tvtotalvalue = inflate.findViewById(R.id.tv_totalvalue);
        TextView tvpoolmoney = inflate.findViewById(R.id.tv_poolmoney);
        TextView tv1 = inflate.findViewById(R.id.tv1);
        TextView tv11 = inflate.findViewById(R.id.tv11);
        TextView tv2 = inflate.findViewById(R.id.tv2);
        TextView tv22 = inflate.findViewById(R.id.tv22);
        //从asset 读取字体
        AssetManager mgr = getAssets();
        //根据路径得到Typeface
        Typeface tf = Typeface.createFromAsset(mgr, "fonts/zzgflqct.ttf");//造字工房朗倩粗体
        tvtotalvalue.setTypeface(tf);
        tvpoolmoney.setTypeface(tf);
        tv1.setTypeface(tf);
        tv11.setTypeface(tf);
        tv2.setTypeface(tf);
        tv22.setTypeface(tf);

        SpannableString totalValue_ss = new SpannableString(totalValue + " ");
        totalValue_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (totalValue + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString pondMoney_ss = new SpannableString(pondMoney + " ");
        pondMoney_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (pondMoney + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString tv1_ss = new SpannableString(getString(R.string.easteregg_hint_nowaward) + " ");
        tv1_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (getString(R.string.easteregg_hint_nowaward) + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString tv11_ss = new SpannableString("Btc ");
        tv11_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, "Btc ".length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString tv2_ss = new SpannableString(getString(R.string.easteregg_hint_totlalvalue) + " ");
        tv2_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (getString(R.string.easteregg_hint_totlalvalue) + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString tv22_ss = new SpannableString("$ ");
        tv22_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, "$ ".length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        tvtotalvalue.setText(totalValue_ss);
        tvpoolmoney.setText(pondMoney_ss);
        tv1.setText(tv1_ss);
        tv11.setText(tv11_ss);
        tv2.setText(tv2_ss);
        tv22.setText(tv22_ss);

        TextView nextHalveNum_tv1 = inflate.findViewById(R.id.tv_nextHalveNum_tv1);
        TextView nextHalveNum_tv3 = inflate.findViewById(R.id.tv_nextHalveNum_tv3);
        TextView nextHalveNum_tv = inflate.findViewById(R.id.tv_nextHalveNum);
        TextView nextHalveNum_tv2 = inflate.findViewById(R.id.tv_nextHalveNum_tv2);
        TextView nextHalveNum_tv5 = inflate.findViewById(R.id.tv_nextHalveNum_tv5);

        nextHalveNum_tv1.setTypeface(tf);
        nextHalveNum_tv3.setTypeface(tf);
        nextHalveNum_tv.setTypeface(tf);
        nextHalveNum_tv2.setTypeface(tf);
        nextHalveNum_tv5.setTypeface(tf);

        SpannableString nextHalveNum_tv1_ss = new SpannableString(getString(R.string.easteregg_hint_nextHalveNum1) + " ");
        nextHalveNum_tv1_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (getString(R.string.easteregg_hint_nextHalveNum1) + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString nextHalveNum_tv3_ss = new SpannableString(getString(R.string.easteregg_hint_nextHalveNum2) + " ");
        nextHalveNum_tv3_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (getString(R.string.easteregg_hint_nextHalveNum2) + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString nextHalveNum_tv_ss = new SpannableString(getString(R.string.easteregg_hint_nextHalveNum3) + " ");
        nextHalveNum_tv_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (getString(R.string.easteregg_hint_nextHalveNum3) + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString nextHalveNum_tv2_ss = new SpannableString(nextHalveNum + " ");
        nextHalveNum_tv2_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (nextHalveNum + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString nextHalveNum_tv5_ss = new SpannableString(getString(R.string.easteregg_hint_nextHalveNum4) + " ");
        nextHalveNum_tv5_ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, (getString(R.string.easteregg_hint_nextHalveNum4) + " ").length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        nextHalveNum_tv1.setText(nextHalveNum_tv1_ss);
        nextHalveNum_tv3.setText(nextHalveNum_tv3_ss);
        nextHalveNum_tv.setText(nextHalveNum_tv_ss);
        nextHalveNum_tv2.setText(nextHalveNum_tv2_ss);
        nextHalveNum_tv5.setText(nextHalveNum_tv5_ss);

        LinearLayout lyQrcodeBodySmall = inflate.findViewById(R.id.ly_qrqdbody_small);
        if (LanguageUtils.getSetIndex() == 1) {
            lyQrcodeBodySmall.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_qd_bg1));
        } else if (LanguageUtils.getSetIndex() == 3) {
            lyQrcodeBodySmall.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_qd_bg3));
        } else if (LanguageUtils.getSetIndex() == 4) {
            lyQrcodeBodySmall.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_qd_bg4));
        } else {
            lyQrcodeBodySmall.setBackground(getResources().getDrawable(R.drawable.icon_eggshare_qd_bg2));
        }
        inflate.findViewById(R.id.leftItem).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (p != null && p.isShowing()) {
                    p.dismiss();
                    p = null;
                }
            }
        });

        TextView tvright = inflate.findViewById(R.id.rightItem);
        tvright.setText("More");
        tvright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopwindow1();
            }
        });


        TextView title = inflate.findViewById(R.id.centerItem);
        title.setText(getString(R.string.easteregg_hint_qdsharename));

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels * 3 / 4;         // 屏幕宽度（像素）
        int height = dm.heightPixels * 3 / 4;       // 屏幕高度（像素）

        p = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        p.setTouchable(true);
        p.setFocusable(true);
        p.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_cccccc)));
        p.setAnimationStyle(R.style.Animleft);
        p.showAtLocation(inflate, Gravity.CENTER, 0, 0);
        p.showAsDropDown(inflate, 100, 100);
        initAddressQRCode();

    }

    PopupWindow popWindow;

    private void showPopwindow1() {
        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View popView = View.inflate(this, R.layout.activity_invite_select, null);

        tvSavebitmap = popView.findViewById(R.id.tv_savebitmap);
        tvCopy = popView.findViewById(R.id.tv_copy);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        if (popWindow == null) {
            popWindow = new PopupWindow(popView, width, Utils.dp2px(this, 130));
//            popWindow.setAnimationStyle(R.style.BottomToTopAnim);
            popWindow.setFocusable(true);
            popWindow.setOutsideTouchable(true);// 设置同意在外点击消失
            tvSavebitmap.setOnClickListener(this);
            tvCopy.setOnClickListener(this);
        }

        ColorDrawable dw = new ColorDrawable(0x30000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    LinearLayout lyQrcodeBody;
    ImageView imgAdressqrcode;
    TextView tvCopy, tvSavebitmap;
    //    private QRCodeEncode.Builder builder;
    private Bitmap qrCodeBitmap = null;
    private Bitmap bodyBitmap = null;
    private String xiazaiuri = "";

    private void initAddressQRCode() {
//        layoutView(lyQrcodeBody,CommonUtil.px2dip(this,470),CommonUtil.px2dip(this,600));

        qrCodeBitmap = CodeCreator.createQRCode(xiazaiuri,
                CommonUtil.dip2px(this, 160),
                CommonUtil.dip2px(this, 160),
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        imgAdressqrcode.setImageBitmap(qrCodeBitmap);

        bodyBitmap = CommonUtil.getBitMapFromView(lyQrcodeBody);
    }


    private void copyWalletAddress(String msg) {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        if (cm != null) {
            // 创建普通字符型ClipData

            ClipData mClipData = ClipData.newPlainText("Label", msg);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
        }
        showToast(getString(R.string.profile_hint_item14));
        tvCopy.setText(getString(R.string.profile_hint_item14));
    }

    /**
     * 保存图片到指定路径
     *
     * @param context
     * @param bitmap   要保存的图片
     * @param fileName 自定义图片名称
     * @return
     */
    public static boolean saveImageToGallery(Context context, Bitmap bitmap, String fileName) {
        //系统相册目录
        String storePath = Environment.getExternalStorageDirectory()
                + File.separator + Environment.DIRECTORY_DCIM
                + File.separator + "Camera" + File.separator;

        LogUtil.e("storePath=" + storePath);
        // 保存图片至指定路径
//        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "qrcode";


        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        File file = new File(appDir, fileName);
        LogUtil.e("file=" + file.toString());
        try {
            LogUtil.e("file1");
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片(80代表压缩20%)
            boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
            fos.flush();
            fos.close();
            LogUtil.e("file2");
            //发送广播通知系统图库刷新数据
            Uri uri = Uri.fromFile(file);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                LogUtil.e("file3");
                return true;
            } else {
                LogUtil.e("file4");
                return false;
            }
        } catch (IOException e) {
            LogUtil.e("file_err=" + e.toString());
            e.printStackTrace();
        }
        LogUtil.e("file5");
        return false;
    }
}
